<?php

class KilangController extends Controller
{
    public $layout='//layouts/column2';

    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function accessRules()
    {
        return array(
            array('allow',
                'actions'=>array('index','read'),
                'users'=>array('*'),
            ),
            array('allow',
                'users'=>array('admin','view'),
            ),
            array('deny',
                'users'=>array('*'),
            ),
        );
    }

    protected function getIndexList($model) {
        $indexList = array(
            array(
		'name' => 'perusahaan_id',
		'value' => '($data->Perusahaan) ? $data->Perusahaan->id : "-"',
	),array(
		'name' => 'provinsi_id',
		'value' => '($data->provinsi_id) ? $data->provinsi_id : "-"',
	),array(
		'name' => 'nama_kilang',
		'value' => '($data->nama_kilang) ? $data->nama_kilang : "-"',
	),array(
		'name' => 'alias',
		'visible' => false,
		'value' => '($data->alias) ? $data->alias : "-"',
	),array(
		'name' => 'kapasitas',
		'visible' => false,
		'value' => '($data->kapasitas) ? $data->kapasitas : "-"',
	),            array(
                'htmlOptions' => array('nowrap'=>'nowrap','class'=>'button-column'),
                'class'=>'booster.widgets.TbButtonColumn'
            ),
        );
        return $indexList;
    }

    protected function getViewList($model) {
        $viewList = array(
            array(
	'name' => 'perusahaan_id',
	'value' => ($model->Perusahaan) ? $model->Perusahaan->id : "-",
),array(
	'name' => 'provinsi_id',
	'value' => ($model->provinsi_id) ? $model->provinsi_id : "-",
),array(
	'name' => 'nama_kilang',
	'value' => ($model->nama_kilang) ? $model->nama_kilang : "-",
),array(
	'name' => 'alias',
	'value' => ($model->alias) ? $model->alias : "-",
),array(
	'name' => 'kapasitas',
	'value' => ($model->kapasitas) ? $model->kapasitas : "-",
),        );
        return $viewList;
    }

    /**
    * Displays a particular model.
    * @param integer $id the ID of the model to be displayed
    */
    public function actionView($id)
    {
        $this->render('view',array(
            'model'=>$this->loadModel($id),
        ));
    }

    public function actionCreate()
    {
        $model=new Kilang;
        $this->performAjaxValidation($model);
        if(isset($_POST['Kilang']))
        {
            $model->attributes=$_POST['Kilang'];
            if($model->save())
                $this->redirect(array($this->defaultAction));
        }
        $this->render('create',array(
            'model'=>$model,
        ));
    }

    public function actionUpdate($id)
    {
        $model=$this->loadModel($id);
        $this->performAjaxValidation($model);
        if(isset($_POST['Kilang']))
        {
            $model->attributes=$_POST['Kilang'];
            if($model->save())
                $this->redirect(array($this->defaultAction));
        }
        $this->render('update',array(
            'model'=>$model,
        ));
    }

    public function actionDelete($id)
    {
        if(Yii::app()->request->isPostRequest)
        {
            $this->loadModel($id)->delete();
            if(!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
            }
        else
            throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
    }

    public function actionIndex()
    {
        $model=new Kilang('publik');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['Kilang']))
            $model->attributes=$_GET['Kilang'];

        $this->render('admin',array(
            'model'=>$model,
        ));
    }

    /**
    * Manages all models.
    */
    public function actionAdmin()
    {
        $model=new Kilang('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['Kilang']))
            $model->attributes=$_GET['Kilang'];

        $this->render('admin',array(
            'model'=>$model,
        ));
    }

    public function loadModel($id)
    {
        $model=Kilang::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

    protected function performAjaxValidation($model)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']==='kilang-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}
